package aexp.customadapter;

import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

class WeatherAdapterView extends LinearLayout {        
        public static final String LOG_TAG = "WeatherAdapterView";

        public WeatherAdapterView(Context context, 
								Weather weather ) {
            super( context );

            this.setOrientation(HORIZONTAL);        
            LinearLayout.LayoutParams cityParams = 
                new LinearLayout.LayoutParams(100, LayoutParams.WRAP_CONTENT);
            cityParams.setMargins(1, 1, 1, 1);

            TextView cityControl = new TextView( context );
            cityControl.setTextAppearance( context, R.style.SpecialText );
			cityControl.setText( weather.getCity() );
            addView( cityControl, cityParams);       

            LinearLayout.LayoutParams temperatureParams = 
                new LinearLayout.LayoutParams(20, LayoutParams.WRAP_CONTENT);
            temperatureParams.setMargins(1, 1, 1, 1);

            TextView temperatureControl = new TextView(context);
            temperatureControl.setText( Integer.toString( weather.temperature ) );
            addView( temperatureControl, temperatureParams);            

            LinearLayout.LayoutParams skyParams = 
                new LinearLayout.LayoutParams(25, LayoutParams.WRAP_CONTENT);

			ImageView skyControl = new ImageView( context );
            Log.d( LOG_TAG, weather.getCity()+" -> "+weather.sky );
			skyControl.setImageResource( weather.getSkyResource() );
			addView( skyControl, skyParams );
        }
}

public class WeatherAdapter extends BaseAdapter {

    private Context context;
    private List<Weather> weatherList;

    public WeatherAdapter(Context context, List<Weather> weatherList ) { 
        this.context = context;
        this.weatherList = weatherList;
    }

    public int getCount() {                        
        return weatherList.size();
    }

    public Object getItem(int position) {     
        return weatherList.get(position);
    }

    public long getItemId(int position) {  
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) { 
        Weather weather = weatherList.get(position);
        return new WeatherAdapterView(this.context, weather );
    }

}
